package com.study.scapp.info.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.scapp.info.common.CommonResult;
import com.study.scapp.info.pojo.InfoUser;
import com.study.scapp.info.service.InfoUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 1
 * @data 2023/5/14
 * 用户
 */
@RequestMapping("/user")
@RestController
public class InfoUserController {

    @Resource
    private InfoUserService infoUserService;

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public CommonResult register(@RequestParam Map<String,Object> user){
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(user);
        InfoUser infoUser = jsonObject.toJavaObject(InfoUser.class);
        QueryWrapper<InfoUser> infoUserQueryWrapper = new QueryWrapper<>();
        infoUserQueryWrapper.eq("login_name",infoUser.getLoginName());
        List<InfoUser> list = infoUserService.list(infoUserQueryWrapper);
        if (list.size() >= 1){
            return CommonResult.failed("该用户已存在！");
        }else {
            infoUserService.save(infoUser);
            return CommonResult.success("注册成功！");
        }

    }

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public CommonResult login(@RequestParam Map<String,Object> user){
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(user);
        InfoUser infoUser = jsonObject.toJavaObject(InfoUser.class);
        QueryWrapper<InfoUser> infoUserQueryWrapper = new QueryWrapper<>();
        infoUserQueryWrapper.eq("login_name",infoUser.getLoginName());
        InfoUser one = infoUserService.getOne(infoUserQueryWrapper);
        if (one == null){
            return CommonResult.failed("用户不存在！");
        }
        if (infoUser.getLoginName().equals(one.getLoginName())){
            if (infoUser.getPassword().equals(one.getPassword())){
                return CommonResult.success(one.getId());
            }else {
                return CommonResult.failed("密码错误！");
            }
        }else {
            return CommonResult.failed("用户不存在！");
        }

    }


    /**
     * 后台登录
     * @param adminUser
     * @return
     */
    @PostMapping("/adminLogin")
    public CommonResult adminLogin(@RequestParam Map<String,Object> adminUser){
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(adminUser);
        InfoUser infoUser = jsonObject.toJavaObject(InfoUser.class);
        String loginName = infoUser.getLoginName();
        String password = infoUser.getPassword();
        if ("liufan".equals(loginName)){
            if ("123456".equals(password)){
                QueryWrapper<InfoUser> infoUserQueryWrapper = new QueryWrapper<>();
                infoUserQueryWrapper.eq("login_name",infoUser.getLoginName())
                        .eq("password",infoUser.getPassword());
                List<InfoUser> list = infoUserService.list(infoUserQueryWrapper);
                if (list.size() >=1){
                    infoUser.setRole(1);
                    infoUserService.save(infoUser);
                    return CommonResult.success("登陆成功！");
                }
                return CommonResult.success("登陆成功！");
            }
            return CommonResult.failed("密码错误！");
        }
        return CommonResult.failed("账号错误！");
    }
}
